﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using ElMexicanos.Base.Models;
using Microsoft.Xna.Framework.Graphics;
using ElMexicanos.Components.TowersTraps.Globals;
using ElMexicanos.Components.TowersTraps;

namespace ElMexicanos.Components.TowersTraps
{
    class TowerManager : Sprite
    {
        List<Tower> _all_towers;

        internal List<Tower> All_towers
        {
            get { return _all_towers; }
            set { _all_towers = value; }
        }

        //// METHODS ///

        public List<Tower> GetTowers()
        {
            return All_towers;
        }

        //Return active towers
        public List<Tower> GetActiveTowers()
        {
            List<Tower> activeTowers = new List<Tower>();
            foreach (Tower tower in All_towers)
            {
                if (tower.State != Tower.TowerState.ownedState)
                {
                    activeTowers.Add(tower);
                }
            }
            return activeTowers;
        }


        ////XNA METHODS ///
        public TowerManager(List<Mob> all_mobs)
        {
            //Instanciation de mes listes
            _all_towers = new List<Tower>();

            //Instanciation des tours
            TowerCactus mycactustower2 = new TowerCactus(new Vector2(500f, 500f));
            TowerCactus mycactustower = new TowerCactus(new Vector2(400f, 500f));
            TowerSpicy mycactustower3 = new TowerSpicy(new Vector2(300f, 500f));

            //les ajoutes à la liste pour les afficher
            All_towers.Add(mycactustower);
            All_towers.Add(mycactustower2);
            All_towers.Add(mycactustower3);

            //Manage chaque tour par son type
            if (All_towers != null)
            {
                foreach (Tower tower in All_towers)
                {                    
                    // no locked mob
                    if(tower.Locked_mob != null)
                        // tower's looking for mobs
                        tower.SearchMob(all_mobs);
                    // locked mob
                    else{
                        tower.Shoot();
                    }
                        
                        

                    if (tower is TowerCactus)
                    {
                        
                        /*
                         * C'est une Tour Cactus
                         * Do something...
                         */

                    }
                    else if (tower is TowerSpicy)
                    {
                        /*
                         * C'est une Tour Cactus
                         * Do something...
                         */
                    }
                }
            }
        }

        public void Initialize()
        {
        }

        public void LoadContent()
        {
            if (All_towers != null)
            {
                foreach (Tower tower in All_towers)
                {
                    tower.LoadContent();
                }
            }
        }

        public void UnloadContent()
        {

        }

        public void Update(GameTime gameTime)
        {
            if (All_towers != null)
            {
                foreach (Tower tower in All_towers)
                {
                    tower.Update(gameTime);
                }
            }
        }

        public void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            if (All_towers != null)
            {
                foreach (Tower tower in All_towers)
                {
                    tower.Draw(spriteBatch, gameTime);
                }
            }
        }
    }
}
